<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="集装箱号 " prop="container">
        <el-input
          v-model="queryParams.container"
          placeholder="请输入集装箱号 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核" prop="auditFlag">
        <el-input
          v-model="queryParams.auditFlag"
          placeholder="请输入审核"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->

    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->

    <!--      </el-col>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="certificateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" width="55" prop="id"/>
      <el-table-column label="进口口岸" align="center" prop="importPort"/>
      <el-table-column label="进口日期" align="center" prop="importDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.importDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经营单位" align="center" prop="operatingUnit"/>
      <el-table-column label="运输方式" align="center" prop="transportationMode"/>
      <el-table-column label="运输工具名称" align="center" prop="transportName"/>
      <el-table-column label="起运国" align="center" prop="shippingCountry"/>
      <el-table-column label="集装箱号 " align="center" prop="container"/>
      <el-table-column label="审核状态" align="center" prop="auditFlag">
        <template slot-scope="scope">

          <el-tag
            v-if="scope.row.auditFlag === 0"
            type="warning"
            @click="handleAudit(scope.row)"
            disable-transitions
          >待处理
          </el-tag>
          <el-tag
            v-else-if="scope.row.auditFlag === 1"
            type="success"
            @click="handleAudit(scope.row)"
            disable-transitions
          >通过
          </el-tag>
          <el-tag
            v-else
            type="info"
            @click="handleAudit(scope.row)"
            disable-transitions
          >不通过
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                    >删除
                    </el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            v-if="scope.row.auditFlag== 0"
            @click="handleAudit(scope.row)"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
        <el-form-item label="进口口岸" prop="importPort">
          <el-input v-model="form.importPort" placeholder="请输入进口口岸"/>
        </el-form-item>
        <el-form-item label="进口日期" prop="importDate">
          <el-date-picker clearable
                          v-model="form.importDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择进口日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经营单位" prop="operatingUnit">
          <el-input v-model="form.operatingUnit" placeholder="请输入经营单位"/>
        </el-form-item>
        <el-form-item label="运输方式" prop="transportationMode">
          <el-input v-model="form.transportationMode" placeholder="请输入运输方式"/>
        </el-form-item>
        <el-form-item label="运输工具名称" prop="transportName">
          <el-input v-model="form.transportName" placeholder="请输入运输工具名称"/>
        </el-form-item>
        <el-form-item label="起运国" prop="shippingCountry">
          <el-input v-model="form.shippingCountry" placeholder="请输入起运国"/>
        </el-form-item>
        <el-form-item label="集装箱号 " prop="container">
          <el-input v-model="form.container" placeholder="请输入集装箱号 "/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框-->
    <el-dialog
      :visible.sync="AuditDialogVisible"
      title="审核"
      width="400px"
    >
      <div>
        <p>当前状态:
          <el-tag
            v-if="auditform.auditFlag=== 0"
            type="warning"
            disable-transitions
          >待处理
          </el-tag>
          <el-tag
            v-else-if="auditform.auditFlag === 1"
            type="success"
            disable-transitions
          >通过
          </el-tag>
          <el-tag
            v-else
            type="info"
            disable-transitions
          >不通过
          </el-tag>
        </p>
        <p>审核状态:
          <el-select v-model="form.auditFlag" placeholder="请选择">
            <el-option
              v-for="item in auditList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
                <el-button @click="AuditDialogVisible= false">取 消</el-button>
                <el-button type="primary" @click="submitAuditForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCertificate,
  delCertificate,
  getCertificate,
  listCertificate,
  updateCertificate
} from '@/api/emsboot/certificate'
import { getOrder, updateOrder } from '@/api/emsboot/order'

export default {
  name: 'Certificate',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库单表格数据
      certificateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uid: null,
        inputFlag: 0,
        importPort: null,
        importDate: null,
        operatingUnit: null,
        transportationMode: null,
        transportName: null,
        shippingCountry: null,
        container: null,
        auditFlag: null
      },
      // 表单参数
      form: {},
      AuditDialogVisible: false,
      auditList:[
        {
          id:0,
          name:"待处理"
        }, {
          id:1,
          name:"通过"
        }, {
          id:2,
          name:"不通过"
        },
      ],
      auditform: {
        auditFlag: null
      },
      // 表单校验
      rules: {
        uid: [
          { required: true, message: '用户id不能为空', trigger: 'blur' }
        ],
        auditFlag: [
          { required: true, message: '审核不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    submitAuditForm(){
      updateCertificate(this.form).then(response => {
        this.msgSuccess('审核成功')
        this.AuditDialogVisible = false
        this.getList()
      })
    },
    handleAudit(row) {
      this.reset()
      const id = row.id || this.ids
      getCertificate(id).then(response => {
        this.form = response.data
        this.AuditDialogVisible = true
        this.auditform.auditFlag  = row.auditFlag
      })

      console.log(row)
    },
    /** 查询入库单列表 */
    getList() {
      this.loading = true
      listCertificate(this.queryParams).then(response => {
        this.certificateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        inputFlag: 0,
        importPort: null,
        importDate: null,
        operatingUnit: null,
        transportationMode: null,
        transportName: null,
        shippingCountry: null,
        container: null,
        auditFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加入库单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCertificate(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改入库单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificate(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCertificate(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除入库单编号为"' + ids + '"的数据项？').then(function() {
        return delCertificate(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('emsboot/certificate/export', {
        ...this.queryParams
      }, `certificate_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
